package com.example.activitytest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {
    private static final String TAG = "FirstActivity";
    private static final int REQUEST_TO_SECONDACTIVITY = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(FirstActivity.this, "You clicked Button1",
                        Toast.LENGTH_SHORT).show();
            }
        });

        //销毁当前活动，相当于按Back键
        Button buttonDes = (Button) findViewById(R.id.button_des);
        buttonDes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //使用显式Intent启动另一个活动
        Button buttonExplicitStart = (Button) findViewById(R.id.button_explicit_start);
        buttonExplicitStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

        //使用隐式Intent启动另一个活动,当注册表中的action与category同时与Intent匹配时，就会启动对应满足条件的活动
        //活动只能有一个action，但能有多个category
        Button buttonImplicitStart = (Button) findViewById(R.id.button_implicit_start);
        buttonImplicitStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                //指定category
                intent.addCategory("com.example.activitytest.My_CATEGORY");

                startActivity(intent);
            }
        });

        //隐式Intent打开web网页
        Button buttonOpenUri = (Button) findViewById(R.id.button_open_uri);
        buttonOpenUri.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "open baidu");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.biadu.com"));
                startActivity(intent);
            }
        });

        //Intent传递字符串
        Button buttonTransStr = (Button) findViewById(R.id.btn_trans_str);
        buttonTransStr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "buttonTransStr");
                String data = "Hello SecondActivity";
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("extra_data", data);
                startActivity(intent);
            }
        });

        //启动并请求SecondActivity的返回数据
        Button buttonGetResult = (Button) findViewById(R.id.btn_get_result);
        buttonGetResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "buttonGetResult");
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivityForResult(intent, REQUEST_TO_SECONDACTIVITY);
            }
        });

    }

    //获取SecondActivity的返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case REQUEST_TO_SECONDACTIVITY:
                if (resultCode == SecondActivity.RESPONSE_TO_FIRSTACTIVITY) {
                    String returnedData = data.getStringExtra("data_return");
                    Log.d(TAG, returnedData);
                }
                break;
            default:
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //加载菜单的布局文件
        getMenuInflater().inflate(R.menu.main, menu);
        //返回true，显示创建的菜单
        return true;
    }

    //添加菜单按钮的监听
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_item:
                Toast.makeText(this,"You clicked Add", Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this,"You clicked Remove", Toast.LENGTH_SHORT).show();
                break;
            default:
        }

        return true;
    }
}